/**
 * @Author: XXHH
 * @CreateTime: 2024-12-05
 */
class Counter{
    // private int count = 0;
    public static int count;
    /*synchronized public void add(){
        count++;
    }
    public int getCount(){
        return count;
    }*/
    public synchronized static void add(){
        count++;
    }
    public static int getCount(){
        return Counter.count;
    }
}
public class Demo19 {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 500000; i++) {
                Counter.add();
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 500000; i++) {
                synchronized (Counter.class){
                    Counter.count++;
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("count:"+Counter.getCount());
    }
}
